<template>
  <div id="home">
    <NavBar class="home-nav"><div slot="center">购物街</div></NavBar>
    <TabControl
        class="tab-control"
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="TabControl1"
        v-show="isTabFixed"
  />
    <Scroll
      class="content"
      ref="scroll"
      :probeType="3"
      :pullUpLoad="true"
      @scroll="contentScroll"
      @pullingUp="LoadMore"
    >
      <HomeSwiper :banners="banners.list" @swiperImageLoad="swiperImageLoad"/>
      <HomeRecommend :recommends="recommends.list" />
      <HomeFeature />
      <TabControl
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="TabControl2"
      />
      <GoodsList :goods="showGoods"></GoodsList>
    </Scroll>
    <BackTop @click.native="backTop" v-show="isShowBackTop" />
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import Scroll from "components/common/scroll/Scroll.vue";

import BackTop from "components/content/backTop/BackTop.vue";
import TabControl from "components/content/tabControl/TabControl.vue";
import { GoodsListItem, GoodsList } from "components/content/goods";

import HomeSwiper from "./childComps/HomeSwiper.vue";
import HomeRecommend from "./childComps/HomeRecommend.vue";
import HomeFeature from "./childComps/HomeFeature.vue";

import { getHomeMultidata, getHomeGoods } from "network/home";

import { debounce } from 'common/utils';
export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommend,
    HomeFeature,
    TabControl,
    GoodsList,
    GoodsListItem,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: {page: 0, list: []},
        new: {page: 0, list: []},
        sell: {page: 0, list: []},
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffsetTop:0,
      isTabFixed:false,
      saveY:0
    };
  },
  created() {
    //1.请求多个数据
    this.getHomeMultidata();
    //2.请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");

  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  mounted() {
    //1.图片加载完成的实践监听
    const refresh = debounce(this.$refs.scroll.refresh,300);
    this.$bus.$on('itemImageLoad', () => {
      refresh()
    });
    //2.获取TabControl的offsetTop
  },
  activated() {
    this.$refs.scroll.scrollTo(0,this.saveY,0);
    this.$refs.scroll.refresh();
  },
  deactivated() {
    this.saveY=this.$refs.scroll.getScrollY()
  },
  methods: {
    /*
      事件监听相关方法
    */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.TabControl1.currentIndex=index;
      this.$refs.TabControl2.currentIndex=index;
    },
    backTop() {
      //先去拿scroll的组件再去拿里面的属性再去调用这个属性的这个方法
      // this.$refs.scroll.scroll.scrollTo(0, 0, 500);
      //拿到这个组件,调用这个组件里的方法
      this.$refs.scroll.scrollTo(0, 0);
    },
    contentScroll(position) {
      //1.判断backtop是否显示
      this.isShowBackTop = -position.y > 1000;
      //2.决定tabControl是否吸顶 position fixed
      this.isTabFixed=(-position.y)>this.tabOffsetTop
    },
    LoadMore() {
      this.getHomeGoods(this.currentType);
      this.$refs.scroll.refresh();
    },
    /*
      网络请求相关方法
     */
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.banner;
        this.recommends = res.recommend;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.list);
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullUp();
      });
    },
    swiperImageLoad(){
      this.tabOffsetTop=this.$refs.TabControl2.$el.offsetTop
    },
  }
};
</script>

<style scoped>
#home {
  /*padding-top: 44px;*/
  position: relative;
  height: 100vh;
}

.home-nav {
  background: var(--color-tint);
  color: #fff;
  /*position: fixed;*/
  /*top: 0;*/
  /*right: 0;*/
  /*left: 0;*/
  /*z-index: 2;*/
}
/*.ul {*/
/*  text-align: center;*/
/*}*/
.content {
  position: absolute;
  overflow: hidden;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  height: 700px;
}
.tab-control{
  position: relative;
  z-index: 2;
}
</style>
